// pages/login/login.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum: '',
    msg: '',
    hidden: true,
    miao: 60
  },
  subMobel: function (e) {
    this.setData({
      phoneNum: e.detail.value,
    })
  },
  subMobel1: function (e) {
    this.setData({
      msg: e.detail.value,
    })
  },
  login: function () {
    let tel = this.data.phoneNum;
    if (tel == '') {
      app.showToastNone('手机号码不可为空', 2000);
      return false;
    }
    let reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!reg.test(tel)) {
      app.showToastNone('请输入正确的手机号', 2000);
      return false;
    }
    let that = this;
    let code = that.data.msg;
    if (!code) {
      app.showToastNone('请输入验证码', 2000); 
      return false;
    }
    wx.request({
      url: app.globalData.host + '/shopuser/login',
      data: {
        tel: tel,
        code: code,
        session_key: app.globalData.userInfo.sessionKey,
        type: 1
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 2000) {
          app.showToastNone('登录成功', 1000);
          setTimeout(function () {
            app.toIndexOnline();
          }, 1000)          
        } else { //返回错误提示信息
          app.showToastNone(res.data.msg, 2000);
        }
      }
    })
  },

  getmsg: function () {
    let tel = this.data.phoneNum;
    if (tel == '') {
      app.showToastNone('手机号码不可为空', 2000);
      return false;
    }
    let reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!reg.test(tel)) {
      app.showToastNone('请输入正确的手机号', 2000);
      return false;
    }
    let that = this;
    let url = app.globalData.host + '/shopuser/send_msg';
    wx.request({
      url: url,
      data: {
        tel: tel
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 2000) {
          that.daojishi();
        } else { //返回错误提示信息
          app.showToastNone(res.data.msg, 2000);
        }
      }
    })
  },
  daojishi: function () {
    let that = this;
    this.setData({
      hidden: !that.data.hidden
    })
    let interval = setInterval(function () {
      that.setData({
        miao: --that.data.miao
      });
      if (that.data.miao == -1) {
        clearInterval(interval);
        that.setData({
          hidden: !that.data.hidden,
          miao: 60
        })
      }
    }, 1000)
  },
})